package com.cicadamob.contact.api.group.asynctask;

import java.security.acl.Group;
import java.util.ArrayList;
import java.util.HashMap;
 
import java.util.Map.Entry;

 








import com.cicadamob.contact.api.Utils.StringUtil;
import com.cicadamob.contact.api.Utils.BizApi.SearchContactsUtil;
import com.cicadamob.contact.api.group.dao.Group_Db_Dao;
import com.cicadamob.contact.api.service.APPContactDao;
import com.cicadamob.contact.api.service.Contact;
import com.cicadamob.contact.api.service.ContactDbHelper;
import com.cicadamob.contact.api.service.SysContactDao;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
 
/**
 * 搜索 同事、客户 、群组成员
 * @author  
 *
 */
public class AsynSearchMemberTask extends AsyncTask<Object, Object, Object>{
	private Context mContext;
	private String keyWorke; //搜索关键字
 	private Handler handler; 
	 
	private int type = -1; //搜索类型
	private String group_id;//群组id 用于搜索某个群组成员
	public AsynSearchMemberTask(int type, Context mContext, String keyWorke,String groupId,
			  Handler handler) {
		super();
		this.type = type;
		this.mContext = mContext;
		this.keyWorke = keyWorke;
 		this.handler = handler;
 		this.group_id=groupId;
 	}

	@Override
	protected Object doInBackground(Object... params) {
		// TODO Auto-generated method stub
	
		switch (type) {
		case SearchContactsUtil.SEARCH_PERSON:
			ArrayList<Contact> resultList = APPContactDao
					.getContactsByWhere(mContext, keyWorke);
			return resultList;
		case SearchContactsUtil.SEARCH_COMPANY_PERSON:

			return ContactDbHelper.getInstance().searchByKey(
					ContactDbHelper.COMPANY_PERSON_DATABASE, keyWorke, 0,
					20,false);

		case SearchContactsUtil.SEARCH_CLIENT_PERSON:
			return ContactDbHelper.getInstance().searchByKey(
					ContactDbHelper.COMPANY_CLIENT_PERSON_TABLE, keyWorke,
					0, 20,false);

		case SearchContactsUtil.SEARCH_GROUP:
			return Group_Db_Dao.getInstance(mContext).queryGroupByName(keyWorke);

		case SearchContactsUtil.SEARCH_GROUP_MEMBER:

			Group_Db_Dao groupDB = Group_Db_Dao.getInstance(mContext);
			return groupDB.queryByContact(keyWorke);

		case SearchContactsUtil.SEARCH_ALL:

			HashMap<Integer, Object> map = new HashMap<Integer, Object>();
			ArrayList<Contact> result = null;

			// 搜索同事
			result = (ArrayList<Contact>) ContactDbHelper
					.getInstance().searchByKey(
							ContactDbHelper.COMPANY_PERSON_DATABASE,
							keyWorke, 0, 20,false);
			map = new HashMap<Integer, Object>();
			map.put(SearchContactsUtil.SEARCH_COMPANY_PERSON, result);
			publishProgress(map);

			// 搜索群组
			result = (ArrayList<Contact>) Group_Db_Dao.getInstance(mContext)
					.queryByContactForGroupSelect(keyWorke,false);
			map = new HashMap<Integer, Object>();
			map.put(SearchContactsUtil.SEARCH_GROUP_MEMBER, result);
			publishProgress(map);

			// 搜索个人
			result = APPContactDao.getContactsByWhere(mContext, keyWorke);
			map = new HashMap<Integer, Object>();
			map.put(SearchContactsUtil.SEARCH_PERSON, result);
			publishProgress(map);

			// 搜索客户
			result = (ArrayList<Contact>) ContactDbHelper
					.getInstance().searchByKey(
							ContactDbHelper.COMPANY_CLIENT_PERSON_TABLE,
							keyWorke, 0, 20,false);
			map = new HashMap<Integer, Object>();
			map.put(SearchContactsUtil.SEARCH_CLIENT_PERSON, result);
			publishProgress(map);
			
			map = new HashMap<Integer, Object>();
			map.put(SearchContactsUtil.SEARCH_COMPLETED, null);
			publishProgress(map);
			

			break;
		case SearchContactsUtil.SEARCH_GROUPID_MEMBER:
			
			if ( !StringUtil.isEmpty(group_id)) {
				Group_Db_Dao groupDB1 = Group_Db_Dao.getInstance(mContext);
				return groupDB1
						.queryByKeywordAndGroupId(group_id, keyWorke);
			} else {
				Group_Db_Dao groupDB1 = Group_Db_Dao.getInstance(mContext);
				return groupDB1.queryByContact(keyWorke);
			}

		}
		
		
		return null;
	}

	@Override
	protected void onPostExecute(Object result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);

		// TODO Auto-generated method stub
		// 执行doInBackground后

		switch (type) {
		case SearchContactsUtil.SEARCH_PERSON:
			ArrayList<Contact> r = (ArrayList<Contact>) result;
			if (r.size() > 0)
			    sendHandlerMsg(r, SearchContactsUtil.SEARCH_PERSON_COMPLETED);
			break;
		case SearchContactsUtil.SEARCH_COMPANY_PERSON:
			ArrayList<Contact> resultCompany = (ArrayList<Contact>) result;
			if (resultCompany.size() > 0)
			    sendHandlerMsg(resultCompany, SearchContactsUtil.SEARCH_COMPANY_PERSON_COMPLETED);
			break;
		case SearchContactsUtil.SEARCH_CLIENT_PERSON:
			ArrayList<Contact> resultClientPerson = (ArrayList<Contact>) result;
			if (resultClientPerson.size() > 0)
			     sendHandlerMsg(resultClientPerson, SearchContactsUtil.SEARCH_CLIENT_PERSON_COMPLETED);
			break;
		case SearchContactsUtil.SEARCH_GROUP:
			ArrayList<Group> resultGroups = (ArrayList<Group>) result;
			if (resultGroups.size() > 0)
			    sendHandlerMsg(resultGroups, SearchContactsUtil.SEARCH_GROUP_COMPLETED);
			break;
		case SearchContactsUtil.SEARCH_GROUP_MEMBER:
			ArrayList<Contact> re = (ArrayList<Contact>) result;
			if (re.size() > 0)
			sendHandlerMsg(re, SearchContactsUtil.SEARCH_GROUP_MEMBER_COMPLETED);
			break;
		case SearchContactsUtil.SEARCH_ALL:

			break;
		case SearchContactsUtil.SEARCH_GROUPID_MEMBER:
			ArrayList<Contact> resultOfGroup = (ArrayList<Contact>) result;
			if (resultOfGroup.size() > 0)
			    sendHandlerMsg(resultOfGroup, SearchContactsUtil.SEARCH_GROUPID_MEMBER_COMPLETED);
			break;

		}
		 sendHandlerMsg("", SearchContactsUtil.SEARCH_COMPLETED);
          
		super.onPostExecute(result);
	
		
	}

	@Override
	protected void onProgressUpdate(Object... values) {

		// TODO Auto-generated method stub
		HashMap<Integer, Object> map = (HashMap<Integer, Object>) values[0];

		for (Entry<Integer, Object> entry : map.entrySet()) {
			// System.out.println(entry.getKey()+"="+entry.getValue());

			ArrayList<Contact> result = (ArrayList<Contact>) entry
					.getValue();
			switch (entry.getKey()) {
			case SearchContactsUtil.SEARCH_PERSON:
				ArrayList<Contact> r = (ArrayList<Contact>) result;
				if (r.size() > 0)
				 //	addPerson(mContext, r, keyWorke);
				   sendHandlerMsg(r, SearchContactsUtil.SEARCH_PERSON_COMPLETED);
				    
				break;
			case SearchContactsUtil.SEARCH_COMPANY_PERSON:
				ArrayList<Contact> resultCompany = (ArrayList<Contact>) result;
				if (resultCompany.size() > 0)
				 	//addCompanyPerson(mContext, resultCompany, keyWorke);
				    sendHandlerMsg(resultCompany, SearchContactsUtil.SEARCH_COMPANY_PERSON_COMPLETED);
				break;
			case SearchContactsUtil.SEARCH_CLIENT_PERSON:
				ArrayList<Contact> resultClientPerson = (ArrayList<Contact>) result;
				if (resultClientPerson.size() > 0)
					// addClientPerson(mContext, resultClientPerson, keyWorke);
				sendHandlerMsg(resultClientPerson, SearchContactsUtil.SEARCH_CLIENT_PERSON_COMPLETED);
				break;
			case SearchContactsUtil.SEARCH_GROUP:
				//  ArrayList<Group> resultGroups=(ArrayList<Group>)result;
				//  addGroupList(mContext, resultGroups);
				//  if(resultGroups.size()>0)
				//  sendHandlerMsg(resultGroups, SearchContactsUtil.SEARCH_GROUP);
				break;
			case SearchContactsUtil.SEARCH_GROUP_MEMBER:
				ArrayList<Contact> re = (ArrayList<Contact>) result;
				if (re.size() > 0)  
				 	//addGroupMember(mContext, re, keyWorke);
				sendHandlerMsg(re, SearchContactsUtil.SEARCH_GROUP_MEMBER_COMPLETED);
				break;
			case SearchContactsUtil.SEARCH_ALL:
                
				break;
			case SearchContactsUtil.SEARCH_COMPLETED:
				sendHandlerMsg("", SearchContactsUtil.SEARCH_COMPLETED);
				break;

			}

		}

		super.onProgressUpdate(values);
	
	}
	
	
	
/**
 * 发handler
 * @param contacts
 * @param what
 */
	private void sendHandlerMsg(Object obj,int what){
		handler.obtainMessage(what, obj).sendToTarget();
	}
	
}
